package com.htandroid.htandroid.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.htandroid.htandroid.R;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

public class MyAdapter_listtab extends BaseAdapter {

    private ArrayList<Map<String, Object>> data;
    private Context context;
    private HashMap<Integer, Boolean> isSelected;
    private int child;//标记哪个子模块

    public MyAdapter_listtab(Context context, int child, ArrayList<Map<String, Object>> data) {
        this.context = context;
        this.child = child;
        this.data = data;
        isSelected = new HashMap<Integer, Boolean>();
    }

    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public Object getItem(int position) {
        return data.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, final ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = LayoutInflater.from(this.context).inflate(getChildLayout(), parent, false);
            holder.tv1 = (TextView) convertView.findViewById(R.id.cvouchid_tv);
            holder.tv2 = (TextView) convertView.findViewById(R.id.cdep_tv);
            holder.tv3 = (TextView) convertView.findViewById(R.id.cnocheckreason_tv);
            holder.tv4 = (TextView) convertView.findViewById(R.id.ddate_tv);
            if (child == 2) holder.tv5 = (TextView) convertView.findViewById(R.id.tv_5);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        Map<String, Object> map = data.get(position);
        holder.tv1.setText(map.get("cvouchid").toString());
        holder.tv2.setText(map.get("cdept").toString());
        holder.tv3.setText(map.get("cnocheckreason").toString());
        holder.tv4.setText(map.get("ddate").toString());
        if (child == 2) holder.tv5.setText(map.get("momney").toString());
        return convertView;
    }

    private int getChildLayout() {
        switch (child) {
            case 1:
                return R.layout.child_1_list_item;
            case 2:
                return R.layout.child_2_list_item;
        }
        return R.layout.child_1_list_item;
    }

    public HashMap<Integer, Boolean> getIsSelected() {
        return isSelected;
    }

    private class ViewHolder {
        TextView tv1;
        TextView tv2;
        TextView tv3;
        TextView tv4;
        TextView tv5;
    }
}